﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Easy.Operation;

namespace Easy.Web.Service.Privilege
{
    /// <summary>
    /// Role 的摘要说明
    /// </summary>
    public class Role : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string json = context.Request["role"];

            string optionType = context.Request["optionType"];

            string optionUser = context.Request["optionUser"];

            ResultClass rc = new ResultClass();

            try
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();

                RoleClass role = jss.Deserialize<RoleClass>(json);


                if (optionType == "1")
                {
                    if (role.Name == "")
                    {
                        rc.Result = 1;
                        rc.Remark = "请提供角色名称";
                    }
                    else if (role.OwningBusinessUnit == "" || role.OwningBusinessUnit.Length != 36)
                    {
                        rc.Result = 1;
                        rc.Remark = "请提供角色的部门ID";
                    }
                    else
                    {
                        string savedid = UserPrivilege_Functions.Role_Save(role, optionType, optionUser);
                        rc.Result = 0;
                        rc.Remark = savedid;
                    }
                }
                else if (optionType == "2" || optionType == "3" || optionType == "4" || optionType == "5")
                {
                    if (role.RoleId == "" || role.RoleId.Length != 36)
                    {
                        rc.Result = 1;
                        rc.Remark = "请提供要操作的角色ID";
                    }
                    else if (optionType == "2" && role.Name == "")
                    {
                        rc.Result = 1;
                        rc.Remark = "修改角色要提供角色名称";

                    }
                    else if (optionType == "2" && (role.OwningBusinessUnit == "" || role.OwningBusinessUnit.Length != 36))
                    {
                        rc.Result = 1;
                        rc.Remark = "修改角色要提供部门ID";

                    }
                    else
                    {
                        string savedid = UserPrivilege_Functions.Role_Save(role, optionType, optionUser);
                        rc.Result = 0;
                        rc.Remark = savedid;
                    }
                }
                else
                {
                    rc.Result = 1;
                    rc.Remark = "错误的操作代码";
                }

              
            }
            catch (Exception e)
            {
                rc.Result = 1;
                rc.Remark = e.Message;
            }


            context.Response.Write(rc.ToJson());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}